package com.jred.jforum.actions;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.jred.jforum.actions.common.TDDispatchAction;
import com.jred.jforum.beans.forum.Forum;
import com.jred.jforum.beans.forum.Message;
import com.jred.jforum.beans.forum.Topic;
import com.jred.jforum.beans.user.User;
import com.jred.jforum.exception.JForumException;
import com.jred.jforum.forms.ProcessTopicForm;
import com.jred.jforum.service.ForumService;
import com.jred.jforum.service.UserService;


public class ShowThreadAction extends TDDispatchAction{

	public ActionForward main(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws JForumException {
		ForumService forumService = (ForumService) getServiceFacade("forumService");
		UserService userService = (UserService) getServiceFacade("userService");
		ProcessTopicForm ptForm = (ProcessTopicForm) form;
		int fid = Integer.parseInt(ptForm.getFid());
		int tid = Integer.parseInt(ptForm.getTid());
		Forum currForum = forumService.getForumInfo(fid);
		Topic currThread = forumService.getThreadInfo(tid);
		HttpSession session = request.getSession();
		
		forumService.updateThreadVcount(currThread.getThreadid());
		List list = forumService.getThreadMessages(tid);
		//回复数
		int num = forumService.getNumReplys(tid);
		
		Iterator it = list.iterator();
		while (it.hasNext()) {
			Message mess = (Message) it.next();
			currThread.setSubject(mess.getHeading());
			//发帖人信息
			mess.setSenderInfo(userService.getSender(mess.getSender()));
		}
		
		
		request.setAttribute("JFORUM_CURR_FORUM", currForum);
		session.setAttribute("JFORUM_CURR_FORUM", currForum);
		request.setAttribute("JFORUM_CURR_TOPIC", currThread);
		request.setAttribute("JFORUM_TOPICE_MESSAGES",list);
		request.setAttribute("JFORUM_NUM_REPLYS",num-1);
		return (mapping.findForward("showThread"));
	}

}
